package com.btpj.shop.config;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2配置类
 *
 * @author BTPJ  2020/8/24
 */
@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    // Api接口包扫描路径
    private static final String BASE_PACKAGE = "com.btpj.shop.controller";

    // 版本号
    private static final String SWAGGER2_API_VERSION = "1.0.0";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档，忽略哪些请求
                .build()
                // 添加登录认证(统一添加token信息)
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    /**
     * API配置信息
     *
     * @return API配置信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("购物系统API文档")
                .description("by BTPJ")
                .version(SWAGGER2_API_VERSION)
                .build();
    }

    /**
     * 请求头信息（可以添加token）登录
     *
     * @return 要添加的请求头信息列表
     */
    private List<ApiKey> securitySchemes() {
        // 设置请求头信息
        return Lists.newArrayList(
                new ApiKey("Authorization", "Authorization", "header")
        );
    }

    private List<SecurityContext> securityContexts() {
        //设置需要登录认证的路径
        List<SecurityContext> result = new ArrayList<>();
        // 所有请求路径
        result.add(getContextByPath("/.*"));
        return result;
    }

    /**
     * 设置需要登录认证的路径
     *
     * @param pathRegex 路径
     * @return SecurityContext
     */
    private SecurityContext getContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> result = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        result.add(new SecurityReference("Authorization", authorizationScopes));
        return result;
    }
}
